﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Backend.Packets;

namespace GameServer.Backend
{
    class PacketParser
    {
        private enum PacketType : short
        {
            CHAT_PACKET,
            ACCOUNT_PACKET,
            SERVER_EVENT_PACKET
        }

        private GameClient m_Client;

        public PacketParser(GameClient Client)
        {
            m_Client = Client;
        }

        public void ParsePacket(byte[] Buffer)
        {
            PacketType PacketType;

            using (MemoryStream ms = new MemoryStream(Buffer))
            {
                BinaryReader br = new BinaryReader(ms);
                br.ReadInt16();
                PacketType = (PacketType)br.ReadInt16();
            }

            IPacket PacketIn;

            //System.Windows.Forms.MessageBox.Show("Received " + ((PacketType)PacketType).ToString());

            switch (PacketType)
            {
                case PacketType.CHAT_PACKET:
                    PacketIn = new ChatPacket();
                    PacketIn.Load(Buffer);

                    GameServer.GetInstance().ChatMgr.ParsePacket((ChatPacket)PacketIn, m_Client);
                    break;

                case PacketType.ACCOUNT_PACKET:
                    PacketIn = new AccountPacket();
                    PacketIn.Load(Buffer);

                    GameServer.GetInstance().AccountMgr.ParsePacket((AccountPacket)PacketIn, m_Client);
                    break;
            }
        }
    }
}
